package com.mkvld.appietijdenv2.views;

import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.data.Utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ViewShift extends LinearLayout {
	double start, end;
	int week, day;
	boolean active = false;
	
	public ViewShift(Context context) {
		super (context);
		setOrientation(LinearLayout.VERTICAL);
		setBackgroundResource(R.drawable.vs_background);
		setClickable(true);
		setSelected(true);
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		
		TextView startTV = new TextView(context);
		startTV.setTextAppearance(context, R.style.SmallTextStyle);
		startTV.setText(Utils.timeToString(-1));
		startTV.setGravity(Gravity.CENTER_HORIZONTAL);
		startTV.setTag("start");
		addView(startTV);
		TextView dashTV = new TextView(context);
		dashTV.setTextAppearance(context, R.style.SmallTextStyle);
		dashTV.setText("-");
		dashTV.setGravity(Gravity.CENTER_HORIZONTAL);
		addView(dashTV);
		TextView endTV = new TextView(context);
		endTV.setTextAppearance(context, R.style.SmallTextStyle);
		endTV.setText(Utils.timeToString(-1));
		endTV.setGravity(Gravity.CENTER_HORIZONTAL);
		endTV.setTag("end");
		addView(endTV);
	}
	
	public ViewShift(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(LinearLayout.VERTICAL);
		setBackgroundResource(R.drawable.vs_background);
		setClickable(true);
		setSelected(true);
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		
		double start = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/com.mkvld.appietijdenv2", "shift_start", -1f);
		double end = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/com.mkvld.appietijdenv2", "shift_end", -1f);
		
		TextView startTV = new TextView(context);
		startTV.setTextAppearance(context, R.style.SmallTextStyle);
		startTV.setText(Utils.timeToString(start));
		startTV.setGravity(Gravity.CENTER_HORIZONTAL);
		startTV.setTag("start");
		addView(startTV);
		TextView dashTV = new TextView(context);
		dashTV.setTextAppearance(context, R.style.SmallTextStyle);
		dashTV.setText("-");
		dashTV.setGravity(Gravity.CENTER_HORIZONTAL);
		addView(dashTV);
		TextView endTV = new TextView(context);
		endTV.setTextAppearance(context, R.style.SmallTextStyle);
		endTV.setText(Utils.timeToString(end));
		endTV.setGravity(Gravity.CENTER_HORIZONTAL);
		endTV.setTag("end");
		addView(endTV);
	}
	
	public void setTimes(double start, double end) {
		this.start = start;
		this.end = end;
		((TextView)findViewWithTag("start")).setText(Utils.timeToString(start));
		((TextView)findViewWithTag("end")).setText(Utils.timeToString(end));
		invalidate();
		requestLayout();
	}
	
	public double getStart() {
		return start;
	}
	public double getEnd() {
		return end;
	}
	
	public boolean isEmpty() {
		return start == -1 && end == -1;
	}
	
	public void setDay(int week, int day) {
		this.week = week;
		this.day = day;
	}
	
	
	public int getWeek() {
		return week;
	}
	public int getDay() {
		return day;
	}
}
